<?php
namespace frontend\controllers;

use Yii;
use yii\helpers\ArrayHelper;
use yii\web\Controller;
use common\models\FnModel;

/**
 * Class RestController
 * @package frontend\controllers
 */
class RestController extends Controller
{

    /**
     * @return array
     */
    public function behaviors()
    {
        $behaviors = [
            'corsFilter' => [
                'class' => \yii\filters\Cors::className(),
                'cors' => [
                    'Origin' => [
                        'http://www.gnogsizhang.com',
                        'https://www.gnogsizhang.com',
                        'http://gsz.ygs.cn',
                        'https://gsz.ygs.cn',
                    ],
                    'Access-Control-Allow-Credentials' => false,
                ]
            ]
        ];
        if (YII_ENV == 'dev' || YII_ENV == 'feature' || YII_ENV == 'prod') {
            $behaviors['corsFilter']['cors'] = [
                'Origin' => ['*'],
                'Access-Control-Request-Method' => ['GET', 'POST'],
                'Access-Control-Request-Headers' => ['*'],
                'Access-Control-Allow-Origin' => ['*'],
                'Access-Control-Allow-Credentials' => false,
                'Access-Control-Max-Age' => 86400,
                'Access-Control-Expose-Headers' => []
            ];
        }
        return ArrayHelper::merge($behaviors,parent::behaviors());
    }

    /**
     * @param array $info
     * @param array $extra 其他杂项数据
     * @throws \yii\base\ExitException
     * 操作成功返回
     */
    public static function successResponse(array $info = [],$extra=[])
    {
        Yii::$app->getResponse()->format = \yii\web\Response::FORMAT_JSON;
        $data = [
            'status'        => 'success',
            'error_code'    => 0,
            'error_message' => '',
            'data'          => $info,
        ];
        if(!empty($extra)) {
            $data['extra'] = $extra;
        }
        Yii::$app->getResponse()->data = $data ;
        Yii::$app->end();
    }

    /**
     * @param array $info
     * @param string $callback
     * @throws \yii\base\ExitException
     * 操作成功返回 jsonP
     */
    public static function successResponseP(array $info = array(), $callback = '')
    {
        Yii::$app->getResponse()->format = \yii\web\Response::FORMAT_JSONP;
        Yii::$app->getResponse()->data = [
            'callback' => $callback,
            'data'     => [
                'status'        => 'success',
                'error_code'    => 0,
                'error_message' => '',
                'data'          => $info
            ]
        ];
        Yii::$app->end();
    }

    /**
     * @param int $code
     * @param null $message
     * @param array $info
     * @throws \yii\base\ExitException
     * 操作失败返回
     */
    public static function failureResponse($code = 500, $message = null, array $info = [])
    {
        Yii::$app->getResponse()->format = \yii\web\Response::FORMAT_JSON;
        Yii::$app->getResponse()->data = [
            'status'        => 'failure',
            'error_code'    => $code,
            'error_message' => $message,
            'data'          => $info
        ];
        Yii::$app->end();
    }

    /**
     * @param int $code
     * @param null $message
     * @param array $info
     * @param string $callback
     * @throws \yii\base\ExitException
     */
    public static function failureResponseP($code = 500, $message = null, array $info = [], $callback = '')
    {
        Yii::$app->getResponse()->format = \yii\web\Response::FORMAT_JSONP;
        Yii::$app->getResponse()->data = [
            'callback' => $callback,
            'data'     => [
                'status'        => 'failure',
                'error_code'    => $code,
                'error_message' => $message,
                'data'          => $info
            ]
        ];
        Yii::$app->end();
    }


    /**
     * 生成签名
     * @param  array    $params
     * @param  string   $secret
     * @return boolean
     */
    protected function makeSign(array $params, $secret) {
        return FnModel::makeSign($params, $secret);
    }

    /**
     * @param $key
     * @param $time
     * @return mixed
     * @throws
     * 访问限制
     */
    protected function preventBrush($key, $time)
    {
        $key = 'Brush:'.$key;
        $getset = Yii::$app->redis->incr($key);
        Yii::$app->redis->expire($key, $time);
        if (false !== $getset && $getset > 1) {
            return self::failureResponse('500','请求太快啦~');
        }
        return true;
    }
}
